package com.lantosec.hmrp.modules.warning.controller;


import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.warning.entity.WarningType;
import com.lantosec.hmrp.modules.warning.service.IWarningTypeService;
import com.lantosec.hmrp.modules.warning.vo.WarningTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 *  控制器
 *
 * @author AutoCode
 * @since 2020-05-19
 */
@Api(value = "预警分类控制器", tags = "预警分类控制器")
@RestController
@RequestMapping("/warning/type")
@Log4j2
public class WarningTypeController extends BaseController {
    @Autowired
    private IWarningTypeService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<WarningTypeVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<WarningTypeVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "新增预警规则分类", type = LogType.update)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "VO对象", required = true) @RequestBody @Validated WarningTypeVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        WarningType entity = vo.convertToEntity();
        service.create(entity);
        return Result.ok(entity.getTypeId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    @ControllerLog(name = "修改预警规则分类", type = LogType.update)
    public Result<Void> update(@ApiParam(value = "VO对象", required = true) @RequestBody WarningTypeVO vo){
        WarningType entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("getWarningTypeTree")
    @ApiOperation("预警类型树")
    public Result<List<TreeNodeVo>> getWarningTypeTree() {
        return Result.ok(service.getWarningTypeTree());
    }
}
